package com.zdsoft.blp.zongbuju.shouye.wuye.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.shouye.wuye.adapter.JiaoFeiAdapter;
import com.zdsoft.blp.zongbuju.shouye.wuye.entity.WuYe;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/30.
 */
public class WYJiaoFeiActivity extends BaseActivity implements NetCallback{
    private FanHuiView back;
    private GridView gv;
    private List<WuYe>list;
    private WuYe wuYe;

    @Override
    protected void init() {
        setContentView(R.layout.wy_jiaofeidating);
        back=find(R.id.back);
        gv=find(R.id.gv);
        back.getTv_title().setText("缴费大厅");

        Net net=new Net();
        net.url= API.BMINFO_URL;
        net.nameSpace=API.BMINFO_NAMESPACE;
        net.methodName=API.COLUMNCHILDLIST;
        Map<String,Object> map=new HashMap<>();
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String communityCode = sp.getString("communityCode", "00001");
        map.put("communityCode",communityCode);
        map.put("columnCode","jf");
        net.parms=map;
        getServer(net,this);

        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String content=list.get(position).getTitle();
                switch (content){
                    case "交水费":
                        Intent intent=new Intent(WYJiaoFeiActivity.this,WYJFDTJiaoFei.class);
                       String url=list.get(position).getContent();
                        intent.putExtra("url",url);
                        Log.e("url",url);
                        startActivity(intent);
                        break;
                    case "交电费":
                        Intent intent2=new Intent(WYJiaoFeiActivity.this,WYJFDTJiaoFei.class);
                        String url2=list.get(position).getContent();
                        intent2.putExtra("url",url2);
                        startActivity(intent2);
                        Log.e("url",url2);
                        break;
                    case "交气费":
                        Intent intent3=new Intent(WYJiaoFeiActivity.this,WYJFDTJiaoFei.class);
                        String url3=list.get(position).getContent();
                        intent3.putExtra("url",url3);
                        Log.e("url",url3);
                        startActivity(intent3);
                        break;
                }
            }
        });
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        Log.e("asfdsdf",res);
        list=jiexi(res);
        JiaoFeiAdapter adapter=new JiaoFeiAdapter(getApplicationContext(),list);
        gv.setAdapter(adapter);
    }

    private List<WuYe> jiexi(String res) {
        list=new ArrayList<>();
        try {
            JSONObject object=new JSONObject(res);
            JSONArray array=object.getJSONArray("value");
            for(int i=0;i<array.length();i++){
                JSONObject object2=array.getJSONObject(i);
                String id=object2.getString("id");
                String name=object2.getString("name");
                String bmColumnImgPath=object2.getString("bmColumnImgPath");
                String url=object2.getString("url");
               wuYe=new WuYe(id,name,url,bmColumnImgPath);
                list.add(wuYe);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }
}
